<?php
include "include/config.inc.php";

if(isset($_POST['Submit']))
{
	$accountIdCr    = (isset($_POST['accountFrom'])) && ($_POST['accountFrom'] != "") ? $_POST['accountFrom'] : 0 ;
	$accountIdDr    = (isset($_POST['accountTo'])) && ($_POST['accountTo'] != "") ? $_POST['accountTo'] : 0 ;
	$transDate      = $_POST['transDateYear']."-".$_POST['transDateMonth']."-".$_POST['transDateDay'];
	$amount         = (isset($_POST['amount'])) && ($_POST['amount'] != "") ? $_POST['amount'] : "" ;
	$narration      = (isset($_POST['narration'])) && ($_POST['narration'] != "") ? $_POST['narration'] : "" ;
	$interest30days = (isset($_POST['interest30days'])) && ($_POST['interest30days'] != "") ? $_POST['interest30days'] : 0;
  $slInterestToPay = "SELECT transactionId, pendingAmount
                        FROM transaction
                       WHERE interest30days > 0
                         AND accountIdCr = ".$accountIdDr."
                         AND pendingAmount > 0 ";
  $slInterestToPayRes = mysql_query($slInterestToPay);	
  $amountCredited = 0;
  while($slInterestToPayRow = mysql_fetch_array($slInterestToPayRes))
  {
    if($amountCredited < $amount)
    {
    	If ($amount <= $slInterestToPayRow['pendingAmount'] )			
      {
        $updateTransection = "UPDATE transaction 
                                 SET pendingAmount = pendingAmount -".$amount."
                               WHERE transactionId = ".$slInterestToPayRow['transactionId'];
        $updateTransectionRes = mysql_query($updateTransection );
        $amountCredited = $amount;

      }
      else
      {
        $updateTransection = "UPDATE transaction 
                                 SET pendingAmount = 0 
                               WHERE transactionId = ".$slInterestToPayRow['transactionId'];
        $updateTransectionRes = mysql_query($updateTransection );
        $amountCredited = $slInterestToPayRow['pendingAmount'] ;
      }
      
    }
  }      
	
  //Here, we store amount in pendingAmount... we just need pendingAmount when interest30days !=0 , but we are storing for all tyep of transaction
  $inserttransaction = "INSERT INTO transaction (accountIdCr,accountIdDr,transDate,amount,pendingAmount,interest30days,narration)
                        VALUES(".$accountIdCr.",".$accountIdDr.",
                               '".$transDate."',
                               ".$amount.",
                               ".$amount.",
                               ".$interest30days.",
                               '".$narration."')";
  $inserttransactionRes = mysql_query($inserttransaction);
  if(!$inserttransactionRes)
    die("Insert Query Not Inserted : ".mysql_error());
  
  ///////////////////////
  if($_POST['interest30days'] != 0 && $_POST['interest30days'] != "")
  {
	  $accountIdCr    = -1; //-1 = Profit Loss
	  $accountIdDr    = 4;  //4  = Interest Paid
	  $accountIdInRef = (isset($_POST['accountFrom'])) && ($_POST['accountFrom'] != "") ? $_POST['accountFrom'] : 0 ;
		
    $inserttransaction1 = "INSERT INTO transaction (accountIdCr,accountIdDr,accountIdInRef,transDate,amount,interest30days,narration)
	                                 VALUES(".$accountIdCr.",".$accountIdDr.",".$accountIdInRef.",
	                                        '".$transDate."',
	                                        ".$interest30days.",
	                                        0,
	                                        '".$narration."')";
	  $inserttransactionRes1 = mysql_query($inserttransaction1);
	  if(!$inserttransactionRes1)
	    die("Insert Query Not Inserted : ".mysql_error());
	  else
	  {
	    header("Location:transaction.php");
	  }
  }
}

$AccArray = array();
$k = 0;
$select = "SELECT accountId,accountName
             FROM account
            ORDER BY accountId";
$selectResult = mysql_query($select);
while($row = mysql_fetch_array($selectResult))
{
 $AccArray['accountId'][$k]   = $row['accountId'];
 $AccArray['accountName'][$k] = $row['accountName'];
 $k++;
}

$smarty->assign('AccArray', $AccArray);
$smarty->display('transaction.tpl');     
?>